package com.update.service.impl;

import com.common.basic.ResponseState;
import com.common.basic.Result;
import com.common.util.StringUtils;
import com.update.dao.ApkInfoDao;
import com.update.model.ApkInfo;
import com.update.service.ApkInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ApkInfoServiceImpl implements ApkInfoService {

    @Autowired
    private ApkInfoDao apkInfoDao;

    @Transactional(readOnly = true)
    @Override
    public Result<List<ApkInfo>> find(ApkInfo apkInfo) {
        Result<List<ApkInfo>> result = new Result();
        if(apkInfo == null || StringUtils.isBlank(apkInfo.getApkName()) || StringUtils.isBlank(apkInfo.getDescription())) {
            result.code = ResponseState.REQUEST_BUSINESS_ERROR;
            result.msg = "参数不能为空";
            return result;
        }
        result.data = apkInfoDao.find(apkInfo);
        result.code = ResponseState.REQUEST_SUCCESS;
        result.msg = "成功";
        return result;
    }

    @Transactional(readOnly = true)
    @Override
    public Result<ApkInfo> findById(Integer apkId) {
        Result<ApkInfo> result = new Result();
        if(apkId == null) {
            result.code = ResponseState.REQUEST_BUSINESS_ERROR;
            result.msg = "参数不能为空";
            return result;
        }
        result.data = apkInfoDao.findById(apkId);
        result.code = ResponseState.REQUEST_SUCCESS;
        result.msg = "成功";
        return result;
    }

    @Transactional
    @Override
    public Result save(ApkInfo apkInfo) {
        Result result = new Result();
        if(apkInfo == null || StringUtils.isBlank(apkInfo.getApkName()) || StringUtils.isBlank(apkInfo.getDescription())) {
            result.code = ResponseState.REQUEST_BUSINESS_ERROR;
            result.msg = "参数不能为空";
            return result;
        }
        apkInfoDao.save(apkInfo);
        result.code = ResponseState.REQUEST_SUCCESS;
        result.msg = "成功";
        return result;
    }

    @Transactional
    @Override
    public Result delete(Integer id) {
        Result result = new Result();
        if(id == null) {
            result.code = ResponseState.REQUEST_BUSINESS_ERROR;
            result.msg = "参数不能为空";
            return result;
        }
        apkInfoDao.delete(id);
        result.code = ResponseState.REQUEST_SUCCESS;
        result.msg = "成功";
        return result;
    }

    @Transactional
    @Override
    public Result update(ApkInfo apkInfo) {
        Result result = new Result();
        if(apkInfo == null || apkInfo.getApkId() == null) {
            result.code = ResponseState.REQUEST_BUSINESS_ERROR;
            result.msg = "参数不能为空";
            return result;
        }
        if(StringUtils.isBlank(apkInfo.getApkName()) || StringUtils.isBlank(apkInfo.getDescription())) {
            result.code = ResponseState.REQUEST_BUSINESS_ERROR;
            result.msg = "参数不能为空";
            return result;
        }
        apkInfoDao.update(apkInfo);
        result.code = ResponseState.REQUEST_SUCCESS;
        result.msg = "成功";
        return result;
    }
}
